import cv2
import dlib

#初始化
predictor = dlib.shape_predictor(r'D:\BaiduNetdiskDownload\face_detect\shape_predictor_68_face_landmarks'
                                 '\shape_predictor_68_face_landmarks.dat')

# 初始化dlib人脸检测器
detector = dlib.get_frontal_face_detector()

# 初始化窗口
win = dlib.image_window()

#cap = cv2.VideoCapture('http://59.37.22.118:1935/live/inside_a967eb81-6873-4293-8ce8-1a84f976f221.flv')
#cap = cv2.VideoCapture('http://121.37.215.12:10080/flv/hls/IsZlhe7MR.flv')
cap = cv2.VideoCapture(0)
while cap.isOpened():
    ok, cv_img = cap.read()
    if not ok:
        break

    img = cv2.cvtColor(cv_img, cv2.COLOR_RGB2BGR)  # 转灰

    dets = detector(img, 1)
    shapes = []
    for k, d in enumerate(dets):
        print("dets{}".format(d))
        print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
            k, d.left(), d.top(), d.right(), d.bottom()))

        # 把人脸框起来
        left = d.left()
        top = d.top()
        right = d.right()
        bottom = d.bottom()
        cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 2)

        # 使用predictor进行人脸关键点识别 shape为返回的结果
        shape = predictor(img, d)
        # shapes.append(shape)
        # 绘制特征点
        for index, pt in enumerate(shape.parts()):
            print('Part {}: {}'.format(index, pt))
            pt_pos = (pt.x, pt.y)
            cv2.circle(img, pt_pos, 1, (0, 225, 0), 2)
            # 利用cv2.putText输出1-68
            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(img, str(index + 1), pt_pos, font, 0.3, (0, 0, 255), 1, cv2.LINE_AA)

    win.clear_overlay()
    win.set_image(img)
    if len(shapes) != 0:
        for i in range(len(shapes)):
            win.add_overlay(shapes[i])
    # win.add_overlay(dets)

cap.release()